/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.context.abstract.FileSystem;
import fever.app.context.abstract.IFileSystem;
import fever.log.FeverDebug;

/**
 * FileSystem using SWF Studio backend.
 * 
 * <p>This filesystem is 100% synchron.
 * 
 * @author Romain Ecarnot
 */
class fever.app.context.swfstudio.StudioFileSystem extends FileSystem implements IFileSystem
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _pluginLoaded : Boolean;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function StudioFileSystem()
	{
		super();
		
		_pluginLoaded = ssCore.Plugin.isPluginLoaded( {plugin:"FILESYS"} ).result == 'true';
		
		if( !_pluginLoaded ) FeverDebug.ERROR( 'You must check the FileSys plugin in SWF Studio IDE' );
	}
	
	/**
	 * Returns {@code true} if passed-in {@code path} is a valid directory 
	 * on filesystem.
	 * 
	 * @param path A fully qualified path
	 */
	public function isDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		var return_obj = ssCore.FileSys.dirExists( { path : path } );

		if ( return_obj.success && return_obj.result == 'TRUE' ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Creates passed-in {@code path} directory on filesystem.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * <p>If any part of the directory path does not exist it will be created.
	 * 
	 * @param path A fully qualified path
	 */
	public function createDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		var return_obj = ssCore.FileSys.createDir( { path : 'path' } );
		if ( return_obj.success ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Removes passed-in {@code path} directory from filesystem.
	 * 
	 * <p>All files insides directory are also deleted.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified path
	 */
	public function deleteDirectory( path : String ) : Boolean
	{
		if( !path ) return false;
		
		var return_obj = ssCore.FileSys.removeDir( {path : path, failIfNotExist : 'true' } );
		if ( return_obj.success ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Renames passed-in {@code path} directory with new passed-in 
	 * {@code newpath} name.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified path
	 * @param newpath A fully qualified path
	 */
	public function renameDirectory( path : String, newName : String ) : Boolean
	{
		if( !path || !newName ) return false;
		
		var return_obj = ssCore.FileSys.renameDir( { path : path, newName : newName } );
		if ( return_obj.success ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Returns {@code true} if passed-in {@code path} is a valid file 
	 * on filesystem.
	 * 
	 * @param path A fully qualified file path
	 */
	public function isFile( path : String ) : Boolean
	{
		if( !path ) return false;
		
		var return_obj = ssCore.FileSys.fileExists( { path : path } );
		if ( return_obj.success && return_obj.result == 'TRUE' ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Renames passed-in {@code path} file with new passed-in 
	 * {@code newName} name.
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified file path
	 * @param newName A fully qualified file path
	 */
	public function renameFile( path : String, newName : String ) : Boolean
	{
		if( !path || !newName ) return false;
		
		var return_obj = ssCore.FileSys.renameFile( { path : path, newName : newName } );
		if ( return_obj.success ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/**
	 * Removes passed-in {@code path} file from filesystem.
	 * 
	 * 
	 * <p>Returns {@code true} if operation ends successfully.
	 * 
	 * @param path A fully qualified file path
	 */
	public function deleteFile( path : String ) : Boolean
	{
		if( !path ) return false;
		
		var return_obj = ssCore.FileSys.deleteFile( { path : path, failIfNotExist : "true" } );
		if ( return_obj.success ) return true;
		else 
		{
			FeverDebug.ERROR( return_obj.Error.description );
			return false;
		}
	}
	
	/** 
	 * Reads file text content.
	 * 
	 * @param fullQualifiedFilename A full qualified filename
	 * 
	 * @return File content or {@code null} if application can't read file.
	 */
	public function readFile( fullQualifiedFilename : String ) : String
	{
		
		if( !fullQualifiedFilename ) 
		{
			FeverDebug.ERROR( 'path can\'t be null or undefined for io.writeToFile()' );
			return null;
		}
		
		var r : Object = ssCore.FileSys.readFile( { path : fullQualifiedFilename } );
		
		if ( r.success  ) return r.result;
		else
		{
			FeverDebug.ERROR( r.Error.description );
			return null;
		}
	}
	
	/**
	 * Writes text data into file.
	 * 
	 * @param path A fully qualified path to the file you want to write.<br />
	 * Default use application starting directory.
	 * @param data String to write in file
	 * 
	 * @return {@code true} if process complete successfully.
	 */
	public function writeToFile( data : String, path : String ) : Boolean
	{
		if( !path ) 
		{
			FeverDebug.ERROR( 'path can\'t be null or undefined for io.writeToFile()' );
			return false;
		}
		
		var r : Object = ssCore.FileSys.writeToFile( { path : path, data : data	} );
		
		if ( r.success  ) return true;
		else
		{
			FeverDebug.ERROR( r.Error.description );
			return false;
		}
	}
	
	/**
	 * Appends text data into file.
	 * 
	 * @param path A fully qualified path to the file you want to write.<br />
	 * Default use application starting directory.
	 * @param data String to write in file
	 *
	 * @return {@code true} if process complete successfully.
	 */
	public function appendToFile( data : String, path : String ) : Boolean
{
		if( !path )
		{
			FeverDebug.ERROR( 'path can\'t be null or undefined for io.writeToFile()' );
			return false;
		}
		
		var r : Object = ssCore.FileSys.appendToFileUTF8( {	path : path, data : data } );
		
		if ( r.success  ) return true;
		else
		{
			FeverDebug.ERROR( r.Error.description );
			return false;
		}
	}
	
	/**
	 * Returns user "temporary" directory  from current system.
	 */
	public function getTempDirectory() : String 
	{
		return ssGlobals.ssWinTempDir;
	}

	/**
	 * Returns current application path from current system.
	 */
	public function getAppDirectory() : String 
	{
		return ssGlobals.ssStartDir;
	}

}